package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rockerhieu.emojicon.parse.ParseMsgUtil;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 帖子列表Adapter
 * 
 * @author
 * 
 */
public class MyPostListAdapter2 extends XinDaoBaseAdapter<Message> {
    private final POSTITEMTYPE mPostItemType;
    private POST_ITEM_TIME_TYPE mPostItemTimeType;
    private OnNextPageListener mOnNextPageListener;
    private boolean isShowCommunity = false;// 是否为圈子首页adapter

    public enum POSTITEMTYPE {
        NORMAL, VIDEO
    }

    public enum POST_ITEM_TIME_TYPE {
        NORMAL, UPDATETIME
    }

    public MyPostListAdapter2(Context context, List<Message> iniData, int pageSize, int res,
            int loadingRes) {
        super(context, iniData, pageSize, res, loadingRes);
        mPostItemType = POSTITEMTYPE.NORMAL;
    }

    public MyPostListAdapter2(Context context, List<Message> iniData, int pageSize, int res,
            int loadingRes, POSTITEMTYPE postItemType) {
        super(context, iniData, pageSize, res, loadingRes);
        mPostItemType = postItemType;
    }

    public void setSortTypeByTime(POST_ITEM_TIME_TYPE type) {
        mPostItemTimeType = type;
    }

    public void setOnNextPageListener(OnNextPageListener listener) {
        mOnNextPageListener = listener;
    }

    @Override
    public void nextPage(int start, int size, ILoadNextPageData<Message> iLoadNextPageData) {
        super.nextPage(start, size, iLoadNextPageData);
        mOnNextPageListener.nextPage(start, size, iLoadNextPageData);
    }

    public interface OnNextPageListener {
        public void nextPage(int start, int size, ILoadNextPageData<Message> iLoadNextPageData);
    }

    @Override
    public View getView(int position, View contentView, ViewGroup parent, final Message post) {
        ViewHolder holder = (ViewHolder) contentView.getTag();

        if (null == holder) {
            holder = new ViewHolder();
            holder.ll = (LinearLayout) contentView.findViewById(R.id.ll_pet_icon);
            holder.usericon = (CircleImageView) contentView.findViewById(R.id.user_icon);
            holder.username = (TextView) contentView.findViewById(R.id.user_name);
            holder.title = (TextView) contentView.findViewById(R.id.tv_title);
            holder.type = (ImageView) contentView.findViewById(R.id.iv_top_right);
            holder.look = (TextView) contentView.findViewById(R.id.look_time);
            holder.message = (TextView) contentView.findViewById(R.id.message_time);
            holder.time = (TextView) contentView.findViewById(R.id.time);
            holder.club_jingording = (ImageView) contentView.findViewById(R.id.club_jingording);
            contentView.setTag(holder);
        }
        holder.title.setVisibility(View.VISIBLE);
        if (holder.ll != null) {
            holder.ll.removeAllViews();
        }

        if (post != null && post.isvideo != null && post.isvideo.equals("1")) {
            View sView = View.inflate(mContext, R.layout.image_pet_icon_shipin, null);
            ImageView iv = (ImageView) sView.findViewById(R.id.pet_icon);
            android.view.ViewGroup.LayoutParams lp1 = iv.getLayoutParams();
            TextView community_title = (TextView) sView.findViewById(R.id.community_title);
            lp1.width = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;
            lp1.height = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;
            iv.setScaleType(ScaleType.CENTER_CROP);
            iv.setLayoutParams(lp1);

            if (CommonParameter.isLoadImg) {
                ImageLoader.getInstance().displayImage(post.videocover, iv);
            } else {
                iv.setImageResource(R.drawable.picture_loading);
            }
            holder.title.setVisibility(View.GONE);
            community_title.setVisibility(View.VISIBLE);
            community_title.setText(ParseMsgUtil.convetToHtml(CommonUtil.parseEmoji(post.subject),
                    mContext));
            LayoutParams lp =
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            sView.setLayoutParams(lp);
            holder.ll.addView(sView);
        } else if (post != null && (post.isvideo == null || post.isvideo.equals("0"))) {
            for (int i = 0; i < post.pics.size(); i++) {
                LinearLayout view1 =
                        (LinearLayout) View.inflate(mContext, R.layout.image_pet_icon, null);
                ImageView iv1 = (ImageView) view1.findViewById(R.id.pet_icon);
                TextView community_title = (TextView) view1.findViewById(R.id.community_title);
                android.view.ViewGroup.LayoutParams lp2 = iv1.getLayoutParams();

                lp2.width = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;
                lp2.height = (CommonParameter.sScreenWidth - CommonUtil.dp2px(mContext, 70)) / 3;
                iv1.setScaleType(ScaleType.CENTER_CROP);
                iv1.setLayoutParams(lp2);
                if (CommonParameter.isLoadImg) {
                    ImageLoader.getInstance().displayImage(post.pics.get(i).url, iv1);
                } else {
                    iv1.setImageResource(R.drawable.picture_loading);
                }
                LayoutParams lp3 =
                        new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                if (post.pics.size() == 1) {
                    holder.title.setVisibility(View.GONE);
                    community_title.setVisibility(View.VISIBLE);
                    community_title.setText(ParseMsgUtil.convetToHtml(
                            CommonUtil.parseEmoji(post.subject), mContext));

                }
                if (i > 0) {
                    lp3.leftMargin = CommonUtil.dip2px(mContext, 5);
                }

                view1.setLayoutParams(lp3);
                holder.ll.addView(view1);
            }
        }
        // if (CommonParameter.isLoadImg) {

        if (post != null) {
            ImageLoader.getInstance().displayImage(post.userface, holder.usericon);
            // } else {
            // holder.usericon.setImageResource(R.drawable.person_loading);
            // }
            holder.username.setText(post.author);

            holder.title.setText(ParseMsgUtil.convetToHtml(CommonUtil.parseEmoji(post.subject),
                    mContext));

            holder.look.setText(post.views + "访问");
            if (isShowCommunity) {
                holder.message.setText(post.forumname);
            } else {
                holder.message.setText(post.replies + "参与");
            }

            String time = CommonUtil.formatDate();
            String year = time.substring(0, 4);
            String hour = time.split(" ")[1].split(":")[0];
            String min = time.split(" ")[1].split(":")[1];

            String mTime = null;
            if (mPostItemTimeType == POST_ITEM_TIME_TYPE.UPDATETIME) {
                mTime = post.updatetime;
            } else {
                mTime = post.addtime;
            }
            String mYear = mTime.substring(0, 4);
            String mMonth = mTime.split(" ")[0].split("-")[1];
            String mDay = mTime.split(" ")[0].split("-")[2];
            String mHour = mTime.split(" ")[1].split(":")[0];
            String mMin = mTime.split(" ")[1].split(":")[1];

            if (year.equals(mYear)) {// 说明是今年
                if (mTime.split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
                    holder.time.setText(mHour + ":" + mMin);
                } else {
                    holder.time.setText(mMonth + "-" + mDay);
                }
            } else {
                holder.time.setText(mTime.split(" ")[0]);
            }

            // switch (currentIndex) {
            // case 1:
            // if (Integer.parseInt(post.digest) > 0) {// 精华
            // holder.type.setBackgroundResource(R.drawable.jing);
            // } else {
            // holder.type.setBackgroundColor(Color.WHITE);
            // }

            // if (!TextUtils.isEmpty(post.displayorder) && Integer.parseInt(post.displayorder) > 0)
            // {
            // // 置顶
            // // holder.type.setBackgroundResource(R.drawable.ding);
            // } else {
            // holder.type.setBackgroundColor(Color.WHITE);
            // }
            int displayorder = 0;
            try {
                displayorder = Integer.parseInt(post.displayorder);
            } catch (Exception e) {
                e.printStackTrace();
            }
            int digest = 0;
            try {
                digest = Integer.parseInt(post.digest);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (displayorder > 0 || digest > 0) {
                holder.club_jingording.setVisibility(View.VISIBLE);
                if (displayorder > 0) {
                    holder.club_jingording.setBackgroundResource(R.drawable.club_ding);
                } else if (digest > 0) {
                    holder.club_jingording.setBackgroundResource(R.drawable.club_jing);
                }
            } else {
                if (holder.club_jingording != null)
                    holder.club_jingording.setVisibility(View.GONE);
            }
        }
        return contentView;
    }

    class ViewHolder {
        LinearLayout ll;
        CircleImageView usericon;
        ImageView club_jingording;
        TextView username;
        TextView title;
        ImageView type;
        TextView look;
        // TextView care;
        TextView message;
        TextView time;
        TextView community_title;
    }

    public void setShowCommunity(boolean isShowCommunity) {
        this.isShowCommunity = isShowCommunity;
    }
}
